package boun.swe.bob.service;

import boun.swe.bob.dao.LikesDao;
import boun.swe.bob.entities.Comment;
import boun.swe.bob.entities.Like;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * The controller for Ajax actions
 */
@RequestMapping("/ajax")
public class AjaxController extends BaseController{

    /**
     * This method gets the users likes
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return likes
     */
    @RequestMapping("/likes.htm")
    public ModelAndView getLikes(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();
        String actionIdStr = httpServletRequest.getParameter("actionId");
        int actionId = 0;
        if(!Strings.isNullOrEmpty(actionIdStr)) {
            actionId = Integer.parseInt(actionIdStr);
        }
        Set<Like> likes = userActionDao.findByPrimaryKey(actionId).getLikes();
        myModel.put("likes", likes);
        return new ModelAndView("likes", "model", myModel);
    }

    /**
     * This method inserts the like to DB
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     */
    @RequestMapping("/like.htm")
    public void insertLike(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String unlike = httpServletRequest.getParameter("unlike");
        String likerUserIdStr = httpServletRequest.getParameter("likerUserId");
        String actionIdStr = httpServletRequest.getParameter("actionId");
        int likerUserId = 0;
        int actionId = 0;
        if(!Strings.isNullOrEmpty(actionIdStr)) {
            likerUserId = Integer.parseInt(likerUserIdStr);
            actionId = Integer.parseInt(actionIdStr);
        }
        if(Strings.isNullOrEmpty(unlike)) {
            if(likerUserId != 0 && actionId != 0) {
                Like like = new Like();
                like.setLikerUserId(likerUserId);
                like.setActionId(actionId);
                likesDao.save(like);
            }
        } else {
            likesDao.deleteLike(likerUserId, actionId);    
        }
    }

    /**
     * This method gets the comments
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return
     */
    @RequestMapping("/comments.htm")
    public ModelAndView getComments(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();
        String actionIdStr = httpServletRequest.getParameter("actionId");
        String method = httpServletRequest.getParameter("method");
        if(null != method && "delete".equals(method)) {
            String commentIdStr = httpServletRequest.getParameter("commentId");
            int commentId = Integer.parseInt(commentIdStr);
            commentsDao.deleteComment(commentId);
            try {
                httpServletResponse.sendRedirect("/bob-web/service/homePage.htm");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        int actionId = 0;
        if(!Strings.isNullOrEmpty(actionIdStr)) {
            actionId = Integer.parseInt(actionIdStr);
        }
        Set<Comment> comments = userActionDao.findByPrimaryKey(actionId).getComments();
        for (Comment comment : comments) {
            if(comment.getCommenterUserId().intValue() == getLoggedInUser(httpServletRequest).getUserId().intValue() ||
               comment.getUserAction().getUserId().intValue() == getLoggedInUser(httpServletRequest).getUserId().intValue()) {
                comment.setDeleteable(true);

            } else {
                comment.setDeleteable(false);
            }
        }
        myModel.put("comments", comments);
        return new ModelAndView("comments", "model", myModel);
    }

    /**
     * This method inserts a comment to DB
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     */
    @RequestMapping("/comment.htm")
    public void comment(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String actionIdStr = httpServletRequest.getParameter("actionId");
        String commenterUserIdStr = httpServletRequest.getParameter("commenterUserId");
        String commentStr = httpServletRequest.getParameter("comment");
        int actionId = 0;
        int commenterUserId = 0;
        if(!Strings.isNullOrEmpty(actionIdStr)) {
            actionId = Integer.parseInt(actionIdStr);
            commenterUserId = Integer.parseInt(commenterUserIdStr);
        }
        Comment comment = new Comment();
        comment.setComment(commentStr);
        comment.setCommenterUserId(commenterUserId);
        comment.setActionId(actionId);
        commentsDao.save(comment);
    }

}
